<!doctype html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>5.姓名案例_watch实现</title>
    <script src="../js/vue.min.js"></script>
</head>

<body>
    <div id="root">
        姓：<input type="text" v-model:value="fristName"> <br>
        名：<input type="text" v-model:value="lastName"> <br>
        姓名：<span>{{fullName}}</span>


    </div>

    <script type="text/javascript">

        const vm = new Vue({
            el: '#root',
            data() {
                return {
                    fristName: '张',
                    lastName: '三',
                    fullName: '张-三'
                }
            },
            watch: {
                fristName(newValue) {
                    setTimeout(() => {
                        this.fullName = newValue + '-' + this.lastName;
                    }, 2000);

                },
                lastName(newValue) {
                    this.fullName = this.fristName + '-' + newValue;
                }
            }
        });

    </script>
</body>

</html>